package com.kuang.controller;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.StrUtil;
import com.kuang.util.ResultModel;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.util.List;


/**
 * 文件上传接口
 * **/
@RestController
@RequestMapping("/user")
public class FileController extends BaseController {

    //文件上传存储路径
    private static final String filePath = System.getProperty("user.dir")+"/file/";

    /**
     * 文件上传
     */
    @PostMapping("/upload")
    public ResultModel upload(MultipartFile file){

        //同步锁，保证在同一时刻上传文件不冲突
        synchronized (FileController.class){
            //获取当前时间戳，防止同名文件被覆盖
            Object flag = System.currentTimeMillis()+"";
            //获取原始文件名（上传的文件本身的名字）
            String fileName = file.getOriginalFilename();


            try {
                //如果没有file文件夹，就会在项目根目录下创建一个file文件夹
                if (!FileUtil.isDirectory(filePath)){
                    FileUtil.mkdir(filePath);
                }
                //文件存储形式：时间戳-文件名
                FileUtil.writeBytes(file.getBytes(),filePath + flag + "-" + fileName);
                System.out.println(fileName+"--上传成功");
                Thread.sleep(1L);
            } catch (Exception e){
                System.err.println(fileName + "--文件上传失败");
            }
            Object filName = flag + "-" + fileName;
            return ResultModel.success(filName);

        }
    }


    /**
     * 获取文件
     * **/
    @GetMapping("/{flag}")
    public void avatarPath(@PathVariable String flag, HttpServletResponse response){
        if (!FileUtil.isDirectory(filePath)){
            FileUtil.mkdir(filePath);
        }
        OutputStream os;
        List<String> fileNames = FileUtil.listFileNames(filePath);
        System.out.println("fileNames"+fileNames);
        String avatar = fileNames.stream().filter(name -> name.contains(flag)).findAny().orElse("");
        System.out.println("avatar"+avatar);
        try {
            if (StrUtil.isNotEmpty(avatar)){
                response.addHeader("Content-Disposition","attachment;filename"+ URLEncoder.encode(avatar,"UTF-8"));
                response.setContentType("application/octet-stream");
                byte[] bytes = FileUtil.readBytes(filePath + avatar);
                os = response.getOutputStream();
                os.write(bytes);
                os.flush();
                os.close();
            }
        }catch (Exception e){
            System.out.println("文件下载失败");
        }
    }
}
